//
//  SettingViewController.m
//  WindRunner
//
//  Created by leeqb on 9/10/14.
//  Copyright (c) 2014 poweropt. All rights reserved.
//

#import "SettingViewController.h"
#import "LoginViewController.h"

@interface SettingViewController ()
{
    CGFloat _rowHeight;
    NSArray *_iconArray;
}
@end

@implementation SettingViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self) {
        _iconArray = @[@"st_login.png", @"st_bound.png", @"st_notification.png", @"st_feedback.png", @"st_about.png", @"st_update.png"];
        
        self.navigationItem.hidesBackButton = YES;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _rowHeight = 0;
    
    _feedbackLabel.text = LocalStringSetting(@"ST_TITLE_FEEDBACK");
    _aboutLabel.text = LocalStringSetting(@"ST_TITLE_ABOUT");
    _updateLabel.text = LocalStringSetting(@"ST_TITLE_UPDATE");
    _notificationLabel.text = LocalStringSetting(@"ST_SETTING_NOTIFICATION");
    [_btnLogout setTitle:LocalStringSetting(@"ST_BTN_LOGOUT") forState:UIControlStateNormal];
    
    _btnLogout.layer.cornerRadius = 4;
    _btnLogout.layer.masksToBounds = YES;
    _notiSwitcher.delegate = self;
    _notiSwitcher.on = [[NSUserDefaults standardUserDefaults] boolForKey:IS_NOTIFICATION_OPEN];
    
    [_headIcon showInCircle];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.title = LocalStringSetting(@"NAV_TITLE_SETTING");
    
    if([AuthHelper isLogin]) {
        NSDictionary *userInfo = [AuthHelper getCurrentUser];
        _loginLabel.text = userInfo[@"account"];
        
        CGRect rect = _footerView.frame;
        rect.origin.y = 0;
        _footerView.frame = rect;
        _settingTable.tableFooterView = _footerView;
        
        LEDownloadManager *downloadManager = [[LEDownloadManager alloc] initWithTarget:_headIcon];
        [downloadManager downLoadImageWithURL:[NSString formatString:userInfo[@"headicon"]] imagetype:@"UserProfileImages"];
    } else {
        _loginLabel.text = LocalStringSetting(@"ST_TITLE_LOGIN");
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 8;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(66, 66);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
    imageView.frame = CGRectMake(0, 0, 66, 66);
    imageView.image = [UIImage imageNamed:@"ninja.png"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    switch (indexPath.row) {
        case 0:
        {
            if([AuthHelper isLogin]) {
                [self performSegueWithIdentifier:@"segUserProfile" sender:nil];
            } else {
                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Security" bundle:nil];
                LoginViewController *vc = (LoginViewController *)[storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
            break;
        }
        case 2:
        {
            UIViewController *vc = [UMFeedback feedbackViewController];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 3:
        {
            [self performSegueWithIdentifier:@"segAbout" sender:nil];
            break;
        }
        case 4:
        {
            [FMToast showLoading:LocalStringCommon(@"OPER_DOING")];
            [MobClick checkUpdateWithDelegate:self selector:@selector(checkNewVersion:)];
            break;
        }
        default:
            break;
    }
}

// 检查新版本
- (void)checkNewVersion:(NSDictionary *)data
{
    if([data[@"update"] boolValue]) {
        [FMToast showMessage:LocalStringSetting(@"VERSION_CHECK_NEW_UPDATE")];
    } else {
        [FMToast showMessage:LocalStringSetting(@"VERSION_CHECK_NO_UPDATE")];
    }
}

- (IBAction)logout:(id)sender {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:LocalStringCommon(@"ALERT_TITLE")
                                                        message:LocalStringSetting(@"ST_CONFIRM_LOGOUT")
                                                       delegate:self
                                              cancelButtonTitle:LocalStringCommon(@"ALERT_CANCEL")
                                              otherButtonTitles:LocalStringCommon(@"ALERT_OK"), nil];
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 1) {
        [AuthHelper logout];
        _loginLabel.text = LocalStringSetting(@"ST_TITLE_LOGIN");
        _settingTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _headIcon.image = [UIImage imageNamed:@"ic_header_def.png"];
    }
}

#pragma mark LESwitcherDelegate
- (void)onSwitch:(BOOL)on // 切换推送开关
{
    [[NSUserDefaults standardUserDefaults] setBool:on forKey:IS_NOTIFICATION_OPEN];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
